from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from datetime import datetime


# Create your models here.
class Menus(models.Model):
    """系统菜单"""
    name = models.CharField(max_length=32, verbose_name='菜单名称')
    href = models.CharField(max_length=100, verbose_name='后端路由', blank=True, null=True)
    method = models.CharField(max_length=10, verbose_name='请求方法', blank=True, null=True)
    icon = models.CharField(max_length=64, verbose_name='菜单图标', blank=True, null=True)
    frontpath = models.CharField(blank=True, null=True, max_length=128, verbose_name='前端路由')
    condition = models.CharField(blank=True, null=True, max_length=128, verbose_name='后端规则')
    desc = models.CharField(max_length=32, default='', verbose_name='请求地址描述')
    order = models.IntegerField(verbose_name='菜单排序', default=50)
    status = models.BooleanField(verbose_name="菜单状态", default=True)
    is_menu = models.BooleanField(verbose_name="是否菜单", default=True)
    parent_id = models.IntegerField(blank=True, null=True, default=0, verbose_name='上级菜单')

    class Meta:
        verbose_name_plural = "菜单"
        ordering = ['order']
        app_label = 'system'  # 添加这一行，指定所属的应用程序标签


class Image(models.Model):
    """图片类"""
    url = models.FileField(max_length=255, upload_to='image/user', verbose_name='图片上传路径')
    type = models.CharField(max_length=20, default=0, verbose_name='图片类型')

    class Meta:
        verbose_name_plural = "图片"
        app_label = 'system'  # 添加这一行，指定所属的应用程序标签


class PhoneCaptcha(models.Model):
    """手机验证码"""
    phone = models.CharField(max_length=12, verbose_name='手机号')
    code = models.CharField(max_length=6, verbose_name='验证码', blank=True, null=True)
    send_time = models.CharField(max_length=20, verbose_name='发送时间', blank=True, null=True)

    class Meta:
        verbose_name = '手机验证码表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.phone}-{self.code}-{str(self.send_time)}"


class Message(models.Model):
    """消息"""
    company_id = models.CharField(max_length=100, verbose_name='公司id 可以为空')
    type = models.SmallIntegerField(verbose_name='消息类型 0平台消息，1公司消息', default=0)
    title = models.CharField(max_length=100, verbose_name='主题')
    content = models.TextField(verbose_name='内容')
    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now, blank=True, null=True)
    creator_user = models.CharField(max_length=100, verbose_name='关联user_id', default='')

    class Meta:
        verbose_name = '消息列表'
        verbose_name_plural = verbose_name


class LabelType(models.Model):
    """标签类型"""
    name = models.CharField(max_length=100, verbose_name='标签类型')

    class Meta:
        verbose_name_plural = "通用标签类型"

    def __str__(self):
        return self.name


class CurrencyLabel(models.Model):
    """素材库通用标签"""
    # label_type = models.ForeignKey(LabelType, blank=True, null=True, related_name='label_types',
    #                                verbose_name='标签类型', on_delete=models.SET_NULL)
    name = models.CharField(max_length=100, verbose_name='标签名称')
    type = models.IntegerField(verbose_name='标签类别',default=0)
    order = models.IntegerField(verbose_name='标签排序', default=50)

    class Meta:
        verbose_name_plural = "通用标签"

    def __str__(self):
        label_type_name = self.label_type.name if self.label_type else 'Unknown Label Type'
        return f"{self.name} ({label_type_name})"

    def delete(self, using=None, keep_parents=False):
        if self.image_labels.count():
            obj, created = CurrencyLabel.objects.get_or_create(name="无标签", label_type=self.label_type, order=-1)
            self.image_labels.update(image_label=obj)
        super().delete()

    def count_image(self):
        return self.image_labels.count()
